package com.itheima.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;

import com.itheima.dto.PagesDto;
import com.itheima.dto.YddAdvimgDto;
import com.itheima.entity.YddAdvimg;
import com.itheima.entity.YddAdvpos;
import com.itheima.service.AdvPosService;
import com.itheima.service.AdvimgService;
import com.itheima.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class AdvimgController {
    @Autowired
    private AdvimgService advimgService;

    @Autowired
    private YddAdvposService yddAdvposService;

    /**
     * 广告图详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddAdvimg> getById(Integer id){
        YddAdvimg advimg = advimgService.getById(id);
        return R.success(advimg);
    }

    /**
     * 广告图分页
     * @param page
     * @param limit
     * @param yddAdvimg
     * @return
     */
    @GetMapping("/list")
    public R<PagesDto<YddAdvimgDto>> list(Integer page, Integer limit, YddAdvimg yddAdvimg){
        log.info("yddAdvimg :{}",yddAdvimg);

        Page<YddAdvimg> pageInfo = new Page<>(page,limit);
        PagesDto<YddAdvimgDto> pageDto = new PagesDto<>();
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(yddAdvimg.getAdvimgdesc() != null && yddAdvimg.getAdvimgdesc()!="",YddAdvimg::getAdvimgdesc,yddAdvimg.getAdvimgdesc());
        queryWrapper.eq(yddAdvimg.getAdvimgpos() != null&& yddAdvimg.getAdvimgdesc()!="",YddAdvimg::getAdvimgpos,yddAdvimg.getAdvimgpos());
        advimgService.page(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        List<YddAdvimg> records= pageInfo.getRecords();



        List<YddAdvimgDto> yddAdvimgDtos = records.stream().map((item)->{
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(item,yddAdvimgDto);
            String advimgposId = item.getAdvimgpos();
            YddAdvpos yddAdvpos = yddAdvposService.getById(advimgposId);
            String advposname = yddAdvpos.getAdvposname();
            yddAdvimgDto.setAdvposname(advposname);
            return yddAdvimgDto;
        }).collect(Collectors.toList());

        //总数
        Long total = pageInfo.getTotal();
        pageDto.setCount(total.intValue());

        //总页数
        Long pages = pageInfo.getPages();
        pageDto.setTotalPages(pages.intValue());

        //当前页码
        Long current = pageInfo.getCurrent();
        pageDto.setCurrentPage(current.intValue());

        //每页数量
        Long size = pageInfo.getSize();
        pageDto.setPageSize(size.intValue());

        pageDto.setData(yddAdvimgDtos);
        return R.success(pageDto);
    }


    /**
     * 广告图修改
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvimg yddAdvimg){
        log.info("yddAdvimg :{}",yddAdvimg);
        advimgService.updateById(yddAdvimg);
        return R.success("修改成功");
    }

    /**
     * 删除广告图
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(Integer id){
        advimgService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 添加广告图
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddAdvimg yddAdvimg){
        advimgService.save(yddAdvimg);
        return R.success("添加成功");
    }
}
